package com.egao.common.article.controller;

import com.egao.common.article.entity.TArticle;
import com.egao.common.article.entity.TArticleType;
import com.egao.common.article.mapper.TArticleTypeMapper;
import com.egao.common.article.service.ITArticleService;
import com.egao.common.article.service.ITArticleTypeService;
import com.egao.common.core.web.BatchParam;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.test.entity.Classify;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @auther ZhaoPeng Wang
 * @create 2022-03-18 23:23:25
 * @description 前端控制器
 */
@Controller
@RequestMapping("/type")
@Slf4j
public class TArticleTypeController {
    @Autowired
    ITArticleService itArticleService;

    @Autowired
    ITArticleTypeService itArticleTypeService;

    @RequestMapping()
    public String view() {

        return "course/article-type.html";
    }

    /**
     * 分页查询
     */
    @ResponseBody
    @RequestMapping("/page")
    public PageResult<TArticleType> page(HttpServletRequest request) {
        PageParam<TArticleType> pageParam = new PageParam<>(request);
        return new PageResult<>(itArticleTypeService.page(pageParam, pageParam.getWrapper()).getRecords(), pageParam.getTotal());
        //return classifyService.listPage(pageParam);  // 使用关联查询
    }

    /**
     * 查询全部
     */
    @ResponseBody
    @RequestMapping("/list")
    public JsonResult list(HttpServletRequest request) {
        PageParam<TArticleType> pageParam = new PageParam<>(request);
        return JsonResult.ok().setData(itArticleTypeService.list(pageParam.getOrderWrapper()));
        //List<Classify> records = classifyService.listAll(pageParam.getNoPageParam());  // 使用关联查询
        //return JsonResult.ok().setData(pageParam.sortRecords(records));
    }

    /**
     * 根据id查询
     */
    @ResponseBody
    @RequestMapping("/get")
    public JsonResult get(Integer id) {
        return JsonResult.ok().setData(itArticleTypeService.getById(id));
        // 使用关联查询
        //PageParam<Classify> pageParam = new PageParam<>();
        //pageParam.put("id", id);
        //List<Classify> records = classifyService.listAll(pageParam.getNoPageParam());
        //return JsonResult.ok().setData(pageParam.getOne(records));
    }

    /**
     * 根据id查询
     */
    @ResponseBody
    @RequestMapping("/getById")
    public String getById(Integer typeId) {
        log.info("类型ID为: {}",typeId);
        TArticleType byId = itArticleTypeService.getById(typeId);
        log.info("类型为: {}",byId.toString());
        return byId.getName();
    }


    /**
     * 添加
     */
    @ResponseBody
    @RequestMapping("/save")
    public JsonResult save(TArticleType type) {
        type.setCreateTime(new Date());
        if (itArticleTypeService.save(type)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    public JsonResult update(TArticleType type) {
        if (itArticleTypeService.updateById(type)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/remove")
    public JsonResult remove(Integer id) {
        if (itArticleTypeService.removeById(id)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

    /**
     * 批量添加
     */
    @ResponseBody
    @RequestMapping("/saveBatch")
    public JsonResult saveBatch(@RequestBody List<TArticleType> list) {
        if (itArticleTypeService.saveBatch(list)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    /**
     * 批量修改
     */
    @ResponseBody
    @RequestMapping("/updateBatch")
    public JsonResult updateBatch(@RequestBody BatchParam<TArticleType> batchParam) {
        if (batchParam.update(itArticleTypeService, "id")) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    /**
     * 批量删除
     */
    @ResponseBody
    @RequestMapping("/removeBatch")
    public JsonResult removeBatch(@RequestBody List<Integer> ids) {
        if (itArticleTypeService.removeByIds(ids)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

}

